package main.java.org.controller;

import lombok.extern.slf4j.Slf4j;
import main.java.Exam.pojo.Entity.Operatorlog;
import main.java.Exam.pojo.Entity.PageResult;
import main.java.Exam.pojo.Entity.User;
import main.java.Exam.pojo.tableDTO.PageDto;
import main.java.Exam.pojo.tableVO.adminVo;
import main.java.org.mapper.AdminMapper;
import main.java.org.mapper.UserMapper;
import main.java.org.result.Result;
import main.java.org.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
@Slf4j

@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    //获取全部学生信息
    @GetMapping("/student/list")
    public Result<PageResult> getStudentdata(PageDto pageDto)
    {log.info("   //获取全部学生信息");
        PageResult pageResult=adminService.getTotalStudent(pageDto);
        return Result.success(pageResult);
    }
    //获取全部老师信息
    @GetMapping("/teacher/list")
    public Result<PageResult> getTeacherdata(PageDto pageDto)
    {log.info("   //获取全部老师信息");
        PageResult pageResult=adminService.getTotalTeacher(pageDto);
        return Result.success(pageResult);
    }
    //添加老师信息
    @PostMapping("/teacher")
    public Result addUser(@RequestBody adminVo adminVo)
    {
        if(adminVo.getUserAccount()!=null){
            throw new RuntimeException("老师已经存在");
        }

        log.info("添加老师信息");
        adminService.insertData(adminVo);
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(adminMapper.selectIDD(adminVo.getUserAccount()));
        operatorlog.setContent("添加老师信息");
        operatorlog.setModule("教师");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("添加");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        adminMapper.insertLog(operatorlog);
        return Result.success();
    }

    //添加学生信息
    @PostMapping("/student")
    public Result addUser1(@RequestBody adminVo adminVo)
    {
        if(adminVo.getUserAccount()!=null){
            throw new RuntimeException("学生已经存在");
        }
        adminService.insertData(adminVo);
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(adminMapper.selectIDD(adminVo.getUserAccount()));
        log.info("添加学生");
    operatorlog.setContent("添加学生信息");
        operatorlog.setModule("学生");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("添加");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        log.info("添加学生信息");
        adminMapper.insertLog(operatorlog);
        return Result.success();
    }

    //修改学生信息
    @PutMapping("/student/{id}/{userAccount}/{roleID}")
    public Result upadteUser(@PathVariable Integer id,@PathVariable String userAccount,@PathVariable Integer roleID)
    {
        log.info("修改学生信息:{}",userAccount);
        adminService.updateData(userAccount,roleID);
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(id);
        operatorlog.setContent("修改学生信息");
        operatorlog.setModule("学生");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("修改");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        adminMapper.insertLog(operatorlog);
        return Result.success();
    }
    //修改老师信息
    @PutMapping("/teacher/{id}/{userAccount}/{roleID}")
    public Result upadteUser1(@PathVariable Integer id,@PathVariable String userAccount,@PathVariable Integer roleID)
    {log.info("修改老师信息:{}",userAccount);
        adminService.updateData(userAccount,roleID);
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(id);
        operatorlog.setContent("修改老师信息");
        operatorlog.setModule("教师");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("修改");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        adminMapper.insertLog(operatorlog);

        return Result.success();
    }
    //删除学生信息
    @DeleteMapping("/student/{id}/{userAccount}")
    public Result deleteUser(@PathVariable Integer id,@PathVariable String userAccount)
    {
        log.info("删除学生信息");
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(id);
        operatorlog.setContent("删除学生信息");
        operatorlog.setModule("学生");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("删除");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        adminMapper.insertLog(operatorlog);
        adminService.deleteData(userAccount);
        return Result.success();
    }
    //删除老师信息
    @DeleteMapping("/teacher/{id}/{userAccount}")
    public Result deleteUser1(@PathVariable Integer id,@PathVariable String userAccount)
    {
        log.info("删除老师信息");
        Operatorlog operatorlog=new Operatorlog();
        User user=adminMapper.getIdUser(id);
        operatorlog.setContent("删除老师信息");
        operatorlog.setModule("教师");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("删除");
        operatorlog.setOperatorAccount(user.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user.getUserName());
        adminMapper.insertLog(operatorlog);
        adminService.deleteData(userAccount);
        return Result.success();
    }
    //查询单个老师信息
    @GetMapping("/teacher/{id}/{userAccount}")
    public Result<User> getSingleteacher(@PathVariable Integer id,@PathVariable String userAccount)
    {log.info("查询单个老师信息");
        User user=adminService.getSingleData(userAccount);
        Operatorlog operatorlog=new Operatorlog();
        User user1=adminMapper.getIdUser(id);
        operatorlog.setContent("查询单个老师信息");
        operatorlog.setModule("教师");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("查询");
        operatorlog.setOperatorAccount(user1.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user1.getUserName());
        adminMapper.insertLog(operatorlog);
        return Result.success(user);
    }
    //查询单个学生信息
    @GetMapping("/student/{id}/{userAccount}")
    public Result<User> getSingleStudent(@PathVariable Integer id,@PathVariable String userAccount)
    {log.info("查询单个学生信息");
        User user=adminService.getSingleData(userAccount);
        Operatorlog operatorlog=new Operatorlog();
        User user1=adminMapper.getIdUser(id);
        operatorlog.setContent("查询单个学生信息");
        operatorlog.setModule("学生");
        operatorlog.setOperationTime(LocalDate.now());
        operatorlog.setOperationType("查询");
        operatorlog.setOperatorAccount(user1.getUserAccount());
        operatorlog.setResult(1);
        operatorlog.setOperatorName(user1.getUserName());
        adminMapper.insertLog(operatorlog);
        return Result.success(user);
    }

    //查询日志
    @GetMapping("/log/list")
    public Result<PageResult> getLog(PageDto pageDto)
    {
        PageResult pageResult=adminService.getLog(pageDto);
        return Result.success(pageResult);
    }

}
